// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Push Button Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.pressed
private alias object.value1 : object.stood

// Player Aliases
private alias object.ypos : player.ypos
private alias object.yvel : player.yvel
private alias object.gravity : player.gravity
private alias object.collisionBottom : player.collisionBottom


// ========================
// Function Declarations
// ========================

reserve function PushButton_DebugDraw
reserve function PushButton_DebugSpawn


// ========================
// Function Definitions
// ========================

private function PushButton_DebugDraw
	DrawSprite(0)
end function


private function PushButton_DebugSpawn
	CreateTempObject(TypeName[Push Button], 0, object.xpos, object.ypos)
end function


// ========================
// Events
// ========================

event ObjectUpdate
	object.pressed = false
	object.stood = false

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.frame == 0
			BoxCollisionTest(C_SOLID, object.entityPos, -14, -6, 14, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				object.stood = true
				object.pressed = true
				player[currentPlayer].ypos += 40000
				PlaySfx(SfxName[Button Press], false)
			end if
		else
			if player[currentPlayer].yvel >= 0
				BoxCollisionTest(C_PLATFORM, object.entityPos, -14, -6, 14, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					object.stood = true
					player[currentPlayer].ypos += 0x20000
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, -20, -12, 20, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
					if checkResult == true
						player[currentPlayer].ypos = player[currentPlayer].collisionBottom
						FlipSign(player[currentPlayer].ypos)
						player[currentPlayer].ypos <<= 16
						player[currentPlayer].ypos += object.ypos
						player[currentPlayer].ypos -= 0x20000
						player[currentPlayer].gravity = GRAVITY_AIR
					end if
				end if
			end if
		end if
	next
end event


event ObjectDraw
	object.frame = object.stood
	DrawSprite(object.frame)
end event


event ObjectStartup
	// We're in OOZ, but load the MPZ sheet?
	// Not only is it the wrong zone, but does this mean the MPZ sheet was done before the OOZ one?
	// With how the rest of the game is structed and coded, it looks like it was primarily started from zone 1 then onwards in level order, but there's a skip here...
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 317, 190)
	SpriteFrame(-16, -4, 32, 8, 317, 190)

	SetTableValue(TypeName[Push Button], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(PushButton_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(PushButton_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	// The button sprites aren't located on the OOZ sheet at all, so..
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 317, 190)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
